##Python实现PCA
import numpy as np
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
from model import KNN
import preprocess
import pandas as pd
from sklearn.model_selection import train_test_split

data1 = pd.read_csv('../wine.csv', sep=',')
data1.columns = ["type"," Alcohol","Malic acid","Ash","Alcalinity of ash"  ,"Magnesium","Total phenols","Flavanoids","Nonflavanoid phenols","Proanthocyanins","Color intensity","Hue","OD280/OD315 of diluted wines","Proline"            ]
Features1 = data1.iloc[:,1:14]
target1 = data1.iloc[:,0]
# 进行数据归一化
scaler = StandardScaler()
Features = scaler.fit_transform(Features1)

#读取鸢尾花的数据：
data2 = pd.read_csv('../iris.data', sep=',')
data2.columns = ["sepal length in cm","sepal width in cm","petal length in cm","petal width in cm","type"]
Features2 = data2.iloc[:,0:4]
target2 = data2.iloc[:,4]
# 进行数据归一化
scaler1 = StandardScaler()
Features1 = scaler1.fit_transform(Features1)

features1 = preprocess.pca(Features,2)
features2 = preprocess.lda(Features,target1,2)

scaler2 = StandardScaler()
Features2 = scaler2.fit_transform(Features2)

# 这里是第一问的结果王小颖别找不到哦
print("pca处理的结果：")
print(features1)# pca处理后的结果
print("lda处理结果：")
print(features2)

X_train,X_test,Y_train,Y_test = train_test_split(Features2,target2,test_size=0.2,random_state=10)

'''enc=preprocessing.LabelEncoder()
enc=enc.fit(['Iris-setosa','Iris-versicolor','Iris-virginica'])
Y_train=enc.transform(Y_train)#使用训练好的LabelEncoder对原数据进行编码
Y_test=enc.transform(Y_test)#使用训练好的LabelEncoder对原数据进行编码'''

#进行训练和预测
model = KNN(x_train=X_train,y_train=Y_train)
print(model.score(x_test=X_test,y_test=Y_test))
#model.draw(x_test=X_test,y_test=Y_test)